<?php

namespace App\Logic\Auth;

use App\Core\BaseCore;
use App\Core\Exception\DefaultException;
use App\Core\Output;
use App\Model\Auth\MenuModel;
use App\Util\ArrayHelper;
use EasySwoole\Component\Singleton;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

class MenuService extends BaseCore
{

    use Singleton;

    /**
     * 字典数据
     * @param $data
     * @return Output
     * @throws Exception
     * @throws Throwable
     */
    public function getDict(): Output
    {
        $result = [
            'type_list'      => ArrayHelper::keyArrayToList(MenuModel::getTypeDict()),
            'menu_type_list' => ArrayHelper::keyArrayToList(MenuModel::getMenuTypeDict()),
        ];

        return $this->success($result);
    }

    /**
     * 删除记录
     * @param $selected
     * @return Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function deleteRecord($selected) : Output{
        is_array($selected) or $selected = [$selected];

        $result = MenuModel::deleteRecord(['id' => $selected]);
        if($result < 1){
            throw new DefaultException('操作失败');
        }

        return $this->success(['affectedRows'=>$result]);
    }


}